package com.yyge.controller.front.contamer;

import cn.hutool.crypto.digest.BCrypt;
import com.yyge.common.controller.BaseController;
import com.yyge.common.enums.AckCode;
import com.yyge.common.vo.resp.PageResp;
import com.yyge.controller.front.contamer.req.ContamerAddReq;
import com.yyge.controller.front.contamer.req.ContamerReq;
import com.yyge.controller.admin.contamer.resp.ContamerResp;
import com.yyge.controller.admin.mapstruct.ContamerConverter;
import com.yyge.entity.Contamer;
import com.yyge.service.ContamerService;
import com.yyge.common.vo.resp.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/front/contamer")
public class FrontContamerController extends BaseController<Contamer,Object> {

    public FrontContamerController(ContamerService service) {
        this.converter = ContamerConverter.class;
    }
    // 注入业务逻辑层

    @Autowired
    private ContamerService service;

    @GetMapping("data")/* GET(SELECT): 查询 */
    public R<PageResp<ContamerResp>> get(ContamerReq req) throws Exception  {
        QueryWrapper<Contamer> wrapper=this.getWrapper(req);
        List<Contamer> list = service.list(wrapper);

        // 将对象进行转换过滤无效条件
        PageResp pageResp =this.page(req,pageInfo ->service.page(pageInfo,wrapper));
        return R.okHasData(pageResp);
    }

    @GetMapping("account/exists")/* GET(SELECT): 判断当前用户是否存在 */
    //http://localhost:8080/api/front/contamer/account/exists?loginid=13139751326
    public R getOne(@Valid String loginid) throws Exception  {
        Contamer contamer=new Contamer();
        contamer.setLoginId(loginid);
        QueryWrapper wrapper=new QueryWrapper<>(contamer);
        long count = service.count(wrapper);
        if(count>0){
            return R.build(AckCode.USER_HAS_ACCOUNT);
        }
        return R.ok();

    }
//
    @PostMapping("register")/* POST(CREATE): 新增 */
    public R postRegister(@RequestBody ContamerAddReq req) throws Exception {
       // Contamer convert = ContamerConverter.MAPPER.convert(req);
        Contamer contamer = service.getById(req.getLoginId());
        System.out.println("对比一下");
        System.out.println(req);

        if(contamer!=null){
            return R.build(AckCode.USER_HAS_ACCOUNT);
        }

        if(!req.getPassword().equals(req.getPasswordConfirm())){
            return R.build(AckCode.SET_PASSWORD_INVALID);
        }

        Contamer convert= ContamerConverter.MAPPER.convert(req);
        System.out.println("转换之后的对象");
        System.out.println(convert);
        convert.setPassword(BCrypt.hashpw(convert.getPassword()));
        convert.setCreateBy(convert.getLoginId());
        convert.setStatus("0");
        boolean result = this.service.save(convert);
        return result?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    @PutMapping/* POST(CREATE): 更新 */
    public R out(@RequestBody Contamer contamer){
        Boolean i=service.updateById(contamer);
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_MODIFY_ERROR);
    }

    @PatchMapping/* PUT(UPDATE): 部分更新 */
    public R patch(@RequestBody Contamer contamer){
        return null;
    }

    @DeleteMapping("/{id}")/* DELETE(DELETE): 删除&&批量删除 */
    public R delete(@PathVariable("id") Integer[] list){
        boolean i = service.removeByIds(Arrays.asList(list));
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_DELETE_ERROR);
    }
}

